<?php

namespace app\admin\controller;

use app\model\ScreenCategory;
use app\model\ScreenModel;
use app\util\ReturnCode;
use Respect\Validation\Exceptions\ValidationException;
use support\Request;
use Respect\Validation\Validator;
/**
 * 大屏管理
 * Class Admin
 * @package app\admin\controller
 */
class Screen extends AdminBase
{
	/**
	 * 大屏列表
	 * @auth false
	 * @login true
	 */
	public function index(Request $request)
	{
		$param = $request->post();
		$list = ScreenModel::where('user_id', $this->user->id);
		if (isset($param['form']['category_id']) && $param['form']['category_id']) {
			$list->where('category_id', $param['form']['category_id']);
		}
		if (isset($param['form']['name']) && $param['form']['name']) {
			$list->whereLike('name', '%'.$param['form']['name'].'%');
		}
		$data = [
			'data'=>$list->select(),
			'total'=>$list->count(),
		];
		return success($data);
	}

	/**
	 * 首页分类列表
	 * @auth false
	 * @login true
	 * @method get
	 */
	public function group()
	{
		$list = ScreenCategory::where('user_id', $this->user->id)->where('type',1)->where('disable', 1)->order('sort desc')->select();
		return success($list);
	}

	/**
	 * 分类管理
	 */
	public function groupedit(Request $request)
	{
		$param = $request->post();
		if (isset($param['id']) && $param['id']){
			$screenCategory = ScreenCategory::where('user_id', $this->user->id)->where('type',1)->where('id', $param['id'])->find();
			if ($screenCategory){
				$data = ScreenCategory::where('user_id', $this->user->id)->where('id', $param['id'])->update(['name'=>$param['name'],'sort'=>$param['sort']]);
			}else{
				return error('参数错误',ReturnCode::UNAUTHORIZED);
			}
		}else{
			$param['user_id'] = $this->user->id;
			$data = ScreenCategory::create($param);
		}
		return success($data);
	}
	public function removeGroup(Request $request)
	{
		$id = $request->post('id');
		$screenCategory = ScreenCategory::where('user_id', $this->user->id)->where('type',1)->where('id', $id)->find();
		if ($screenCategory){
			$data = ScreenCategory::where('user_id', $this->user->id)->where('id', $id)->delete();
			return success($data);
		}else{
			return error('参数错误',ReturnCode::UNAUTHORIZED);
		}
	}

	/**
	 * 创建大屏
	 */
	public function create(Request $request)
	{
		$param = $request->post();
		try {
			Validator::input($param, [
				'name' => Validator::NotEmpty()->setName('请输入大屏名称'),
				'category_id' => Validator::NotEmpty()->setName('请选择分类')
			]);
		} catch (ValidationException $e) {
			return error($e->getMessage(),500);
		}
		$param['config'] = $param['layout'];
		$param['user_id'] = $this->user->id;
		$param['thumb'] = '//files.pengxiaotian.com/datav/bj.png';
		if (isset($param['id'])){
			$data = ScreenModel::update($param);
		}else{
			$data = ScreenModel::create($param);
		}
		return success($data['id']);
	}

	/**
	 * 查看大屏
	 * @auth false
	 * @login true
	 * @method get
	 */
	public function detail(Request $request)
	{
		$id = $request->post('id');
		$data = ScreenModel::where('user_id', $this->user->id)->where('id', $id)->find();
		if (!$data) {
			return error( '参数错误',301);
		}
		return success($data);
	}

	/**
	 * 删除大屏
	 * @auth false
	 * @login true
	 * @method get
	 */
	public function del()
	{
		$id = \request()->post('id');
		$data = ScreenModel::where('user_id', $this->user->id)->where('id', $id)->find();
		if (!$data) {
			return error('参数错误',301);
		}
		$data = ScreenModel::destroy($id,true);
		return success($data);
	}

	/**
	 * 过滤器查询
	 * @auth false
	 * @login true
	 * @method get
	 */
	public function filter()
	{
		$data = [];
		return $this->successCode($data);
	}

	/**
	 * 复制大屏
	 * @auth false
	 * @login true
	 * @method get
	 */
	public function copy()
	{
		$param = input();
		$data = DataProject::where('user_id', Admin::id())->where('id', $param['id']);
		if ($data) {
			$data = DataProject::create($param);
		} else {
			return $this->errorCode(301, '参数错误');
		}
		return $this->successCode($data['id']);
	}
}
